package com.tansun.easycare.modules.sys.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jeedev.msdp.base.entity.SysUser;
import com.jeedev.msdp.sys.entity.SysRole;
import com.tansun.easycare.core.utils.CacheUtils;
import com.tansun.easycare.modules.sys.service.RoleService;
import com.tansun.easycare.modules.sys.service.SysCommService;
import com.tansun.easycare.modules.sys.service.SysUserService;
import com.tansun.easycare.rule.exception.BizException;
import com.tansun.easycare.rule.exception.DataEmptyException;

@Service
public class SysCommServiceImpl implements SysCommService {
	public static final String USER_CACHE = "sysCache";
	public static final String USER_CACHE_ID_ = "id_";
	public static final String USER_CACHE_LOGIN_NAME_ = "ln_";
	public static final String USER_CACHE_LIST_BY_OFFICE_ID_ = "oid_";

	public static final String CACHE_ROLE_LIST = "roleList";
	public static final String CACHE_MENU_LIST = "menuList";
	public static final String CACHE_AREA_LIST = "areaList";
	public static final String CACHE_OFFICE_LIST = "officeList";
	public static final String CACHE_OFFICE_ALL_LIST = "officeAllList";
	
	@Autowired
	private  CacheUtils  cacheUtils;
	
	@Autowired
	private  SysUserService  sysUserService;
	
	@Autowired
	private  RoleService  roleService;
	
	
	
	@Override
	public   SysUser  getCurrUser() {
		 return  sysUserService.getCurrUser();
	}

	/**
	 * 根据ID获取用户
	 * @param id
	 * @return 取不到返回null
	 */
	@Override
	public  SysUser get(String id){
		SysUser user = 	(SysUser)cacheUtils.get(USER_CACHE, USER_CACHE_ID_ + id);
		if (user ==  null){
			user = sysUserService.getUserById(id);
			if (user == null){
				return null;
			}
			cacheUtils.put(USER_CACHE, USER_CACHE_ID_ + id, user);
		}
		return user;
	}
	
	/**
	 * 根据登录名获取用户
	 * @param loginName
	 * @return 取不到返回null
	 */
	@Override
	public  SysUser getByLoginName(String loginName){
		SysUser user = 	(SysUser)cacheUtils.get(USER_CACHE, USER_CACHE_ID_ + loginName);
		if (user ==  null){
			user = sysUserService.findUserByLoginNum(loginName);
			if (user == null){
				return null;
			}
			cacheUtils.put(USER_CACHE, USER_CACHE_ID_ +loginName, user);
		}
		return user;
	}
	
	/**
	 * 根据登录中文名名获取用户信息
	 * @param realName
	 * @return 取不到返回null
	 */
	@Override
	public  SysUser getByRealName(String realName){
		SysUser user = 	(SysUser)cacheUtils.get(USER_CACHE, USER_CACHE_ID_ + realName);
		if (user ==  null){
			user = sysUserService.findUserByRealNum(realName);
			if (user == null){
				throw new BizException("创建人和修改人查询只支持精确查询，请检查输入的内容是否正确！");
 			  //return null;
			}
			cacheUtils.put(USER_CACHE, USER_CACHE_ID_ +realName, user);
		}
		return user;
	}
	
	/**
	 * 清除当前用户缓存
	 */
	@Override
	public  void clearCache(){
		removeCache(CACHE_ROLE_LIST);
		removeCache(CACHE_MENU_LIST);
		removeCache(CACHE_AREA_LIST);
		removeCache(CACHE_OFFICE_LIST);
		removeCache(CACHE_OFFICE_ALL_LIST);

	}
	
	/**
	 * 清除指定用户缓存
	 * @param user
	 */
	@Override
	public  void clearCache(SysUser user){
		cacheUtils.remove(USER_CACHE, USER_CACHE_ID_ + user.getId());
		cacheUtils.remove(USER_CACHE, USER_CACHE_LOGIN_NAME_ + user.getLoginName());
	}
	
	/**
	 * 获取当前用户
	 * @return 取不到返回 new User()
	 */
	@Override
	public  SysUser getUserById(String id){
		//TODO 默认值
		SysUser user = 	(SysUser)cacheUtils.get(USER_CACHE, USER_CACHE_ID_ + id);
		if (user ==  null){
			user = sysUserService.getUserById(id);
			if (user == null){
				return new SysUser();
			}
			cacheUtils.put(USER_CACHE, USER_CACHE_ID_ +id, user);
		}
		return user;
	}

	/**
	 * 获取当前用户角色列表
	 * @return
	 */
	@Override
	public  List<SysRole> getRoleList(){
		@SuppressWarnings("unchecked")
		List<SysRole> roleList = (List<SysRole>)getCache(CACHE_ROLE_LIST);

		if (roleList == null){
			roleList= this.roleService.findRoleList();
			putCache(CACHE_ROLE_LIST, roleList);
		}
	
		return roleList;
	}
	

	@Override
    public  Object getCache(String key) {
        return getCache(key, null);
    }
	@Override
    public  Object getCache(String key, Object defaultValue) {
    	return cacheUtils.get(key, defaultValue);

    }
    @Override
    public  void putCache(String key, Object value) {
    	cacheUtils.put(key, value);
    }
    @Override
    public  void removeCache(String key) {
    	cacheUtils.remove(key);  
    }

    @Override
    public  List<SysUser> getUserListByRole(String roleName) {
//        User user = new User();
//        user.setDelFlag("0");
        return  null;
    }

    //工作流
    public static final String TENANT_ID_KEY = "tenantIdKey";
    /**
     * 获取当前用户所属租户
     */
    public  void  setTenantCache(String tenantKey){
        putCache(TENANT_ID_KEY, tenantKey);
    }
    public  String getTenantCache(){
        return (String)getCache(TENANT_ID_KEY);
    }
}
